var objId =  getQueryString("objId");
var $;
layui.config({
	base : "js/"
}).use(['form','layer','jquery'],function(){
	var form = layui.form(),
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage;
		$ = layui.jquery;
    //初始化加载数据
    $.ajax({
        url: API_URL_HEAD+"ticketRecycleRecord/selectTicketRecycleById/"+objId,
        type: 'get',
        async: false,
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (data) {
        	var vo = data.data;
        	$(".studentName").val(vo.studentName);
        	$(".studentSn").val(vo.studentSn);
 			$(".oldTicketNum").val(vo.oldTicketNum);
 			$(".ticketNum").val(vo.ticketNum);
 			//初始化加载水票下拉列表
    		getOldTicketList(vo.oldTicketInfo,vo.ticketId);
        }
			   });
    
 	form.on("submit(addUser)",function(data){
 		var dto = {
			"id":objId,
			"status":3,
			"oldTicketInfo":$(".oldTicketName").val(),
			"oldTicketNum":$(".oldTicketNum").val(),
			"ticketId":$(".ticketName").val(),
			"ticketNum":$(".ticketNum").val()
		};
		$.ajax({
			        url: API_URL_HEAD+"ticketRecycleRecord/auditTicktRecycleRecord",
			        type: 'post',
			        async: false,
			        data:JSON.stringify(dto),
			        contentType: 'application/json; charset=utf-8',
			        dataType: 'json',
			        success: function (data) {
			        	if(data.code == 0){
			        		//刷新父页面
			 				parent.location.reload();
			 			}
			        }
			   });
 		return false;
 	})
/*
	 *供应商列表
	 */
	function getOldTicketList(oldId,newId){
		var dto = {};
	      $.ajax({
	      	    type: "POST",
	      	    dataType: "json",
	      	    url : API_URL_HEAD+"waterTicket/select",
	      	    contentType: "application/json; charset=utf-8",
	      	    async:false,
	      	    data:JSON.stringify(dto),
	      	  	success: function (data) {  
	      	  		if(data.code == "0"){
		      	  		var jsArr = data.data;
		      	  		//区分电子水票和纸质水票
		      	  		var oldTicketArr = [];
		      	  		var newTikcetArr = [];
		      	  		for(var i in jsArr)
		      	  		{
		      	  			if(jsArr[i].ticketType == 1)
		      	  			{
		      	  				oldTicketArr.push(jsArr[i]); 
		      	  			}else{
		      	  				newTikcetArr.push(jsArr[i]);
		      	  			}
		      	  		}
		      	  		var newTicketObj = $(".ticketName");
		      	  		var oldTicketObj = $(".oldTicketName");
		      	  		
		      	  		createSelectOptions(newTicketObj,newTikcetArr,"ticketName",newId);
		      	  		createSelectOptions(oldTicketObj,oldTicketArr,"ticketName",oldId);
		      	  		form.render("select");
	      	  		}
	      	    },
	      	    error: function(data) { 
	      	    	console.log(data);
	      	    	layer.alert("服务器忙，请稍后重试");
	      	    }
	      	});	
	}
	//生成select options
	function createSelectOptions(selObj,dataArray,name,id){
		for(var i in dataArray){
			var every = dataArray[i];
			if(id==undefined){
				$(selObj).append("<option value='"+every["id"]+"'>"+every[name]+"</option>");
			}else{
				if(every["id"]==id){
					$(selObj).append("<option value='"+every["id"]+"'  selected=\"selected\">"+every[name]+"</option>");
				}else{
					$(selObj).append("<option value='"+every["id"]+"'>"+every[name]+"</option>");
				}
			}
			
		}
	}
})

//格式化时间
function formatTime(_time){
    var year = _time.getFullYear();
    var month = _time.getMonth()+1<10 ? "0"+(_time.getMonth()+1) : _time.getMonth()+1;
    var day = _time.getDate()<10 ? "0"+_time.getDate() : _time.getDate();
    var hour = _time.getHours()<10 ? "0"+_time.getHours() : _time.getHours();
    var minute = _time.getMinutes()<10 ? "0"+_time.getMinutes() : _time.getMinutes();
    return year+"-"+month+"-"+day+" "+hour+":"+minute;
}

//获取url中的参数
function getQueryString(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); // 匹配目标参数
  var result = window.location.search.substr(1).match(reg); // 对querystring匹配目标参数
  if (result != null) {
    return decodeURIComponent(result[2]);
  } else {
    return null;
  }
}